package cn.interestingshop.dao;

import cn.interestingshop.entity.BaseOrder;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class BaseOrderDaoImpl extends BaseDaoImpl implements BaseOrderDao{
    public BaseOrderDaoImpl(Connection connection) {
        super(connection);
    }

    @Override
    public BaseOrder createEntityByResultSet(ResultSet rs) throws Exception {

        BaseOrder baseOrder=new BaseOrder();

        if(rs.next()){
            baseOrder.setId(rs.getInt("id"));
            baseOrder.setOrderNo(rs.getString("order_no"));
            baseOrder.setAmount(rs.getFloat("amount"));
            baseOrder.setUserAddress(rs.getString("user_address"));
            baseOrder.setAccount(rs.getString("account"));
            baseOrder.setUserId(rs.getInt("user_id"));
            baseOrder.setZfOk(rs.getInt("zfOk"));

        }

        return baseOrder;
    }


    public List<BaseOrder> createEntityByResultSet2(ResultSet rs) throws Exception {

        List<BaseOrder> list=new ArrayList<>();

        while (rs.next()){

            BaseOrder baseOrder=new BaseOrder();
            baseOrder.setId(rs.getInt("id"));
            baseOrder.setOrderNo(rs.getString("order_no"));
            baseOrder.setAmount(rs.getFloat("amount"));
            baseOrder.setUserAddress(rs.getString("user_address"));
            baseOrder.setAccount(rs.getString("account"));
            baseOrder.setUserId(rs.getInt("user_id"));
            baseOrder.setZfOk(rs.getInt("zfOk"));

            list.add(baseOrder);
        }

        return list;
    }

    @Override
    public int insert(BaseOrder baseOrder) {
        int rs=0;

        try {
            String sql = "INSERT INTO `interesting_shop_db`.`t_base_order`(`user_id`, `account`, `user_address`,`amount`, `order_no`) VALUES (?,?,?,?,?)";
            Object[] objects = new Object[]{baseOrder.getUserId(),baseOrder.getAccount(),baseOrder.getUserAddress(),baseOrder.getAmount(),baseOrder.getOrderNo()};
            rs = executeInsert(sql, objects);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeResource();
        }

        return rs;
    }

    @Override
    public BaseOrder selectOne(int id) {
        ResultSet rs=null;
        BaseOrder baseOrder=new BaseOrder();

        String sql="select * from t_base_order where id="+id;
        rs=executeQuery(sql,null);
        try {
            baseOrder=createEntityByResultSet(rs);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeResource();
            closeResource(rs);
        }
        return baseOrder;
    }


    @Override
    public List<BaseOrder> selectOneAll(int id,Integer currentPageNo, Integer pageSize) {
        ResultSet rs=null;
        List<BaseOrder> baseOrder=new ArrayList<>();

        String sql="select * from t_base_order a where a.user_id=?";
        sql += " limit " + (currentPageNo - 1) * pageSize + "," + pageSize;
        Object[] objects=new Object[]{id};
        rs=executeQuery(sql,objects);
        try {
            baseOrder=createEntityByResultSet2(rs);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeResource();
            closeResource(rs);
        }
        return baseOrder;
    }

    @Override
    public List<BaseOrder> selectAll(Integer currentPageNo, Integer pageSize) {
        ResultSet rs=null;
        List<BaseOrder> baseOrder=new ArrayList<>();

        String sql="select * from t_base_order";
        sql += " limit " + (currentPageNo - 1) * pageSize + "," + pageSize;
        rs=executeQuery(sql,null);
        try {
            baseOrder=createEntityByResultSet2(rs);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeResource();
            closeResource(rs);
        }
        return baseOrder;
    }

    @Override
    public int selectOneAllCount(int id) {
        ResultSet resultSet=null;
        int rs=0;

        String sql="select count(1) from t_base_order where user_id="+id;
        resultSet=executeQuery(sql,null);
        try {
            if(resultSet.next()){
                rs=resultSet.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeResource();
            closeResource(resultSet);
        }
        return rs;
    }

    @Override
    public int selectAllCount() {
        ResultSet resultSet=null;
        int rs=0;

        String sql="select count(1) from t_base_order";
        resultSet=executeQuery(sql,null);
        try {
            if(resultSet.next()){
                rs=resultSet.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeResource();
            closeResource(resultSet);
        }
        return rs;
    }

    @Override
    public int xgZf(String orderNo) {

        int rs=0;
        try {
        String sql="update t_base_order set zfOk=0 where order_no=?";
        Object[] objects=new Object[]{orderNo};
        rs=executeUpdate(sql,objects);


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeResource();

        }
        return rs;
    }
}
